<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <245369209@qq.com> 
// +----------------------------------------------------------------------

namespace app\service\controller\api;
use app\common\controller\Api;
use think\response\Json;

/**
 * 标准规范管理
 * @package app\standard
 * @author LJQ<245369209@qq.com>
 */
class Standard extends Api

{

    /**
   *  标准规范列表
   * @param string 
   * @return Json
   */

	public function standardList($standardId=null){
             $where['typeId'] = ['eq',2] ;

            if ($standardId) {
             $where['id']=['eq',$standardId]; 
             }
		//分页参数
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			// 标准规范列表
			$info =collection(model('standard')->with('user')->where($where)->limit($startLimit.','.$limit)->select())->toArray();
		}else{
			//标准规范列表
			$info =collection(model('standard')->with('user')->where($where)->select())->toArray();
		}
	   	 
	 	//总数
	   	 $count = model('standard')->where($where)->count('id'); 
		
    		      if ($info && $count == 0) {
         			return json(['code'=>1,'msg'=>'没有找到标准规范','count'=>$count,'data'=>$info]);
       		} 
       	
       		if ($count > 0){
       			return json(['code'=>1,'msg'=>'标准规范','count'=>$count,'data'=>$info]);
       		}
            		return json(['code'=>0,'msg'=>'数据异常']);
   	 	 }
   	  
   	      //添加标准规范
   	 public function add($data){
          
          if (empty($data)) {
              return json(['code'=>0,'msg'=>'数据异常,请检查']);
          }
          $model = model('service/standard');
          $result = $model->change($data);
          if (false != $result) {
             setlog('standardadd','standard',session('user_auth.id'));
              return json(['code'=>1,'msg'=>'标准规范添加成功']);
          } else {
              return json(['code'=>0,'msg'=>'标准规范添加失败,请检查']);
          }
      }

   	

   /**
   * 修改标准规范
   * @param  string standardId 标准规范id,
   * @return Json
   */
   	public function edit($data,$standardId){
        
          if (empty($standardId)) {
              return json(['code'=>0,'msg'=>'标准规范不存在,请检查']);
          }
          $model = model('service/standard');     
          if ($model->change($data)) {
             setlog('standardupdate','standard',session('user_auth.id'));
             return json(['code'=>1,'msg'=>'标准规范修改成功']);
            }else{
             return json(['code'=>0,'msg'=>'标准规范信息修改失败,请检查']);
            }
    }
    /**
   *  删除标准规范
   * @param string standardId 标准规范id,
   * @param Json
   */
     
    public function del($standardId) {
        $standardId = input('standardId',0,'intval');
        $model = model('service/standard');
      	if (empty ( $standardId )||empty(($model->where(array('id'=>$standardId))->find()))) {
           	return  json(['code'=>0,'msg'=>'标准规范id不存在,请检查']);
        }
        if($model->destroy(['id'=>$standardId]) !== FALSE){
          setlog('standarddel','standard',session('user_auth.id'));
        	 return json(['code'=>1,'msg'=>'标准规范删除成功']);
       }else{
         	 return $this->json(['code'=>0,'msg'=>'标准规范删除失败,请检查']);
       }
    }

     
   /**
   *   查询标准规范
   * @param keyword标准规范名称
   * @return Json
   */
   	public function search($keyword=null){
   		$where['typeId'] = 2;

		if($keyword){
			
			$where['title'] = ['like','%'.$keyword.'%'];
		}


   		//分页参数
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			//标准规范列表
			$info =collection(model('standard')->with('user')->where($where)->limit($startLimit.','.$limit)->select())->toArray();
		}else{
			//标准规范列表
			$info =collection(model('standard')->with('user')->where($where)->select())->toArray();
		}
	 	//总数
	       $count = model('standard')->where($where)->count('id'); 
		 
    	       if (empty($info) && $count == 0) {
              	return json(['code'=>0,'msg'=>'没有找到标准规范','count'=>$count]);
       	} 
       	
       	if ($count > 0){
       		return json(['code'=>1,'msg'=>'标准规范','count'=>$count,'data'=>$info]);
       	}
      }
} 